import configparser
import os


def init_config(env):
    file_path = '../../base_info.ini'

    file = file_path.replace('\"', '')
    judge_file = os.path.exists(file)

    if judge_file:
        config = configparser.ConfigParser()
        config.read(file, encoding='utf-8')

        if env == 'test':
            environment = config['environment'].get('test')
            return environment
        if env == 'dev':
            environment = config['environment'].get('dev')
            return environment
        if env == 'pro':
            environment = config['environment'].get('pro')
            return environment
        else:
            return None
    else:
        print('file not exist')
        raise ValueError('文件不存在')


def get_test_taskid():
    file_path = '../config/test_task_id.ini'

    file = file_path.replace('\"', '')
    judge_file = os.path.exists(file)

    if judge_file:
        config = configparser.ConfigParser()
        config.read(file, encoding='utf-8')

        taskid = config['testTaskId'].get('test_task_id')
        if taskid == '':
            return None
        return taskid

    else:
        print('file not exist')
        raise ValueError('文件不存在')


def save_test_taskid(test_taskid):
    file_path = '../config/test_task_id.ini'

    file = file_path.replace('\"', '')
    judge_file = os.path.exists(file)

    if judge_file:
        config = configparser.ConfigParser()
        section = 'testTaskId'
        config.add_section(section)
        config.set(section, 'test_task_id', str(test_taskid))

        # 写入
        with open(file_path, 'w') as configfile:
            config.write(configfile)

    else:
        print('file not exist')
        raise ValueError('文件不存在')